package ipad.asia.controller;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import ipad.asia.config.BeanCopy;
import ipad.asia.entity.Enterprise;
import ipad.asia.entity.Product;
import ipad.asia.entity.ProductCopy;
import ipad.asia.service.enterprise.EnterpriseFeign;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.service.productCopy.ProductCopyFeign;
import ipad.asia.utils.FtpUtil;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
public class ProductController {
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private EnterpriseFeign enterpriseFeign;
    @Autowired
    private ProductCopyFeign productCopyFeign;
    @RequestMapping("/product/list")
    public String productList(HttpServletRequest request, Model model) throws Exception {
        HttpSession session=request.getSession();
        List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
        model.addAttribute("products",products);
        return "productList";
    }


    @RequestMapping("/product/detail")
    public String productDetail(@RequestParam(value = "productId",required = false)int productId,
                                HttpServletRequest request, Model model) throws Exception {
        Product product = productFeign.getById(productId);
        model.addAttribute("product",product);
        return "productDetail";
    }

    @RequestMapping("/product/update")
    public String productUpdate(@RequestParam(value = "productName",required = false) String productName,
                                @RequestParam(value = "price",required = false) String price,
                                @RequestParam(value = "describe",required = false) String describe,
                                @RequestParam(value = "photo",required = false) MultipartFile photo,
                                @RequestParam(value = "kind",required = false) String kind,
                                @RequestParam(value = "color",required = false) String color,
                                @RequestParam(value = "number",required = false) String number,
                                @RequestParam(value = "productId",required = false)String productId,
                                @RequestParam(value = "haveDiscount",required = false)String haveDiscount,
                                Model model,HttpServletRequest request) throws Exception {
        HttpSession session=request.getSession();
        System.out.println("kind:::::"+kind);
        String newKind=null;
        if(productId==null){
            List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products",products);
            return "productList";
        }
        if(productName==null){
            Product product1=productFeign.getById(Integer.parseInt(productId));
            model.addAttribute("product", product1);
            return "productUpdate";
        }
        else {
            String s="默认";
            System.out.println(kind.equals(s));
            System.out.println(kind);
            if (kind==s) {
                Product product=productFeign.getById(Integer.parseInt(productId));
                newKind = product.getKind();
                System.out.println("newKind:"+newKind);
            } else {
                newKind = kind;
                System.out.println("newKind:"+newKind);
            }

            if(haveDiscount==null){
                haveDiscount=0+"";
            }
            String salt = enterpriseFeign.getName((String) session.getAttribute("user")).getSalt();
            FtpUtil.uploadFile(photo.getOriginalFilename(),photo.getInputStream());
            productFeign.update(productName, Float.parseFloat(price), Integer.parseInt(number), color, newKind, Integer.parseInt(haveDiscount), photo.getOriginalFilename(), Integer.parseInt(productId));
            Product product1=productFeign.getById(Integer.parseInt(productId));
            model.addAttribute("product", product1);
            model.addAttribute("message", "修改成功！");
            return "productUpdate";
        }
    }

    /**
     * 下架商品，复制到下架表后删除
     * @param model
     * @param session
     * @return
     * @throws Exception
     */

    @RequestMapping("/product/under")
    public String productUnder(@RequestParam(value = "productId",required = false)String productId1,
                                Model model,HttpSession session) throws Exception {
        if(productId1==null){
            List<ProductCopy> products = productCopyFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products",products);
            return "productUnder";
        }else {
            int productId = Integer.parseInt(productId1);
            Product product = productFeign.getById(productId);
            System.out.println(product);
            ProductCopy productCopy = new ProductCopy();
            BeanUtils.copyProperties(product, productCopy);
            System.out.println(productCopy);
            productCopyFeign.addObj(productCopy);
            productFeign.DeleteById(productId);
            List<ProductCopy> products = productCopyFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products", products);
            model.addAttribute("message", product.getName() + "商品下架成功！");
            return "productUnder";
        }
    }


    @RequestMapping("/product/delete")
    public String productDelete(@RequestParam(value = "productId",required = false)String productId,
                                Model model,HttpSession session) throws Exception {
        if(productId==null){
            List<ProductCopy> products = productCopyFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products",products);
            model.addAttribute("message","没有该商品，请刷新后重试！");
            return "productUnder";
        }else {
            productCopyFeign.DeleteById(Integer.parseInt(productId));
            List<ProductCopy> products = productCopyFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products", products);
            model.addAttribute("message", "商品删除成功！");
            return "productUnder";
        }
    }

    @RequestMapping("/product/add")
    public String productAdd( @RequestParam(value = "productName",required = false) String productName,
                              @RequestParam(value = "price",required = false) String price,
                              @RequestParam(value = "describe",required = false) String describe,
                              @RequestParam(value = "photo",required = false) MultipartFile photo,
                              @RequestParam(value = "kind",required = false) String kind,
                              @RequestParam(value = "color",required = false) String color,
                              @RequestParam(value = "photo1",required = false) MultipartFile photo1,
                              @RequestParam(value = "photo2",required = false) MultipartFile photo2,
                              @RequestParam(value = "photo3",required = false) MultipartFile photo3,
                              @RequestParam(value = "photo4",required = false) MultipartFile photo4,
                              @RequestParam(value = "desc_photo1",required = false) MultipartFile desc_photo1,
                              @RequestParam(value = "desc_photo2",required = false) MultipartFile desc_photo2,
                              @RequestParam(value = "desc_photo3",required = false) MultipartFile desc_photo3,
                              @RequestParam(value = "desc_photo4",required = false) MultipartFile desc_photo4,
                              @RequestParam(value = "desc_photo5",required = false) MultipartFile desc_photo5,
                              @RequestParam(value = "number",required = false) String number,
                              Model model,HttpServletRequest request,HttpSession session) throws IOException {
        if(price==null&&number==null){
            return "productAdd";
        }else {
           String salt = enterpriseFeign.getName((String) session.getAttribute("user")).getSalt();
            FtpUtil.uploadFile(photo.getOriginalFilename(),photo.getInputStream());
            FtpUtil.uploadFile(photo1.getOriginalFilename(),photo1.getInputStream());
            FtpUtil.uploadFile(photo2.getOriginalFilename(),photo2.getInputStream());
            FtpUtil.uploadFile(photo3.getOriginalFilename(),photo3.getInputStream());
            FtpUtil.uploadFile(photo4.getOriginalFilename(),photo4.getInputStream());
            FtpUtil.uploadFile(desc_photo1.getOriginalFilename(),desc_photo1.getInputStream());
            FtpUtil.uploadFile(desc_photo2.getOriginalFilename(),desc_photo2.getInputStream());
            FtpUtil.uploadFile(desc_photo3.getOriginalFilename(),desc_photo3.getInputStream());
            FtpUtil.uploadFile(desc_photo4.getOriginalFilename(),desc_photo4.getInputStream());

            System.out.println("productName" + productName);
            System.out.println("price" + price);
            System.out.println("describe" + describe);
            System.out.println("photo" + photo);
            System.out.println("kind" + kind);
            System.out.println("color" + color);
            System.out.println("number" + number);

            String name = (String) session.getAttribute("user");
            Enterprise enterprise = enterpriseFeign.getName(name);
            System.out.println("shopId" + enterprise.getId());
            Product product = new Product();
            product.setName(productName).setShopId(enterprise.getShopId()).setPrice(Float.parseFloat(price)).setDescribes(describe)
                    .setPhoto(photo.getOriginalFilename()).setKind(kind).setColor(color).setPhoto1(photo1.getOriginalFilename()).setPhoto2(photo2.getOriginalFilename())
                    .setPhoto3(photo3.getOriginalFilename()).setPhoto4(photo4.getOriginalFilename()).setDescPhoto1(desc_photo1.getOriginalFilename()).setDescPhoto2(desc_photo2.getOriginalFilename())
                    .setDescPhoto3(desc_photo3.getOriginalFilename()).setDescPhoto4(desc_photo4.getOriginalFilename()).setDescPhoto5(desc_photo5.getOriginalFilename()).setNumber(Integer.parseInt(number));
            productFeign.addObj(product);
//        productFeign.add(productName,price,describe,photo,enterprise.getShopId(),kind,color
//        ,photo,);
            model.addAttribute("message", "添加商品成功！");
            return "productAdd";
        }
    }

    @RequestMapping("/product/up")
    public String productUp(@RequestParam(value = "productId",required = false)String productId1,
                               Model model,HttpSession session) throws Exception {
        if(productId1==null){
            List<ProductCopy> products = productCopyFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products",products);
            return "productUnder";
        }else {
            int productId = Integer.parseInt(productId1);
            ProductCopy productCopy=productCopyFeign.getById(productId);
            Product product=new Product();
            BeanUtils.copyProperties(productCopy,product);
            productFeign.addObj(product);
            productCopyFeign.DeleteById(productId);

            List<ProductCopy> products = productCopyFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products", products);
            model.addAttribute("message", product.getName() + "商品上架成功！");
            return "productUnder";
        }
    }

    @Test
    public void test(){
        String s="默认";String j="默认";
        System.out.println(s.equals(j));
    }

}
